package com.axiang.module_work.factory;

import android.annotation.SuppressLint;
import android.app.Application;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.axiang.module_work.model.WorkModel;
import com.axiang.module_work.viewmodel.WorkViewModel;

public class WorkViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile WorkViewModelFactory INSTANCE;
    private final Application mApplication;

    public static WorkViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (WorkViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WorkViewModelFactory(application);
                }
            }
        }
        return INSTANCE;
    }
    private WorkViewModelFactory(Application application) {
        this.mApplication = application;
    }
    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(WorkViewModel.class)) {
            return (T) new WorkViewModel(mApplication, new WorkModel(mApplication));
        }

        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
